<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="公告标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入公告标题"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="发布状态" prop="scheduleStatus">
        <el-select
          v-model="queryParams.scheduleStatus"
          placeholder="发布状态"
          clearable
          style="width: 240px"
        >
          <el-option label="待发布" value="1" />
          <el-option label="已发布" value="0" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['notice:noticePage:noticeAdd']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['notice:noticePage:noticeEdit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['notice:noticePage:noticeDel']"
        >删除</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:dict:export']"
            >导出</el-button>
      </el-col>-->

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-row :gutter="20">
      <el-col :span="4">
        <el-card style="min-height:400px" class="box-card">
          <div class="df aic jcsb">
            <span>公告分类</span>
            <i
              @click="openType"
              v-hasPermi="['notice:noticePage:typeAdd']"
              class="el-icon-circle-plus-outline fs20"
              style="color:#409EFF"
            ></i>
          </div>

          <div class="mt10">
            <div
              class="df aic jcsb pl10 pr10 fs14"
              :class="typeIndex == item.bulletinTypeId ? 'typeActive' : ''"
              v-for="(item,index) in bulletinTypeData"
              :key="index"
            >
              <div style="min-width:50%" @click.stop="hendType(item)">{{item.bulletinTypeName}}</div>
              <div class>
                <el-button
                  type="text"
                  v-hasPermi="['notice:noticePage:typeEdit']"
                  @click.stop="openType(item)"
                >编辑</el-button>
                <el-button
                  type="text"
                  v-hasPermi="['notice:noticePage:typeDel']"
                  @click.stop="handleTypeDelete(item)"
                  style="color:red"
                >删除</el-button>
              </div>
            </div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="20">
        <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="公告标题" align="center" prop="title" :show-overflow-tooltip="true" />
          <el-table-column label="发布人" align="center" prop="publisherName" />
          <el-table-column label="可见范围" align="center" prop="visibility">
            <template slot-scope="scope">
              <span>{{ scope.row.visibility == 0 ? '全部可见' : '部分可见' }}</span>
            </template>
          </el-table-column>
          <el-table-column label="发布状态" align="center" prop="scheduleStatus">
            <template slot-scope="scope">
              <span v-if="scope.row.scheduleStatus == 1">待发布</span>
              <span v-if="scope.row.scheduleStatus == 0">已发布</span>
            </template>
          </el-table-column>
          <!-- <el-table-column label="已读" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ scope.row.readNum }}</span>
            </template>
          </el-table-column>
          <el-table-column label="未读" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{scope.row.unreadNum}}</span>
            </template>
          </el-table-column>-->
          <el-table-column label="发布时间" align="center" prop="createTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['notice:noticePage:edit']"
              >修改</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['notice:noticePage:del']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>

        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </el-col>
    </el-row>

    <el-dialog :title="bulletinTypeId ? '编辑公告类型' : '新增公告类型' " :visible.sync="dialogFormVisible">
      <el-form :model="typeForm">
        <el-form-item label="类型名称">
          <el-input v-model="typeForm.bulletinTypeName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="typeConfig">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="60%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入公告标题" />
        </el-form-item>

        <el-form-item label="分类" prop="category">
          <el-select v-model="form.category" placeholder="分类" clearable>
            <el-option
              v-for="(item,index) in bulletinTypeData"
              :key="index"
              :label="item.bulletinTypeName"
              :value="item.bulletinTypeId"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="可见范围" prop="visibilityType">
          <el-radio @change="form.visibilityList = []" v-model="form.visibilityType" label="0">全部可见</el-radio>
          <el-radio @change="form.visibilityList = []" v-model="form.visibilityType" label="1">部分可见</el-radio>
        </el-form-item>
        <el-form-item label="选择可见员工" v-if="form.visibilityType == 1" prop="visibilityList">
          <el-cascader
            :show-all-levels="false"
            ref="subject"
            v-model="form.visibilityList"
            :options="subjectList"
            :props="supplierSubjectsIdProps"
            :style="{ width: '50%' }"
            placeholder="请选择可见员工"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="附件" prop="file">
          <fileUpload v-model="form.file" />
        </el-form-item>

        <el-form-item label="公告内容" prop="content">
          <Editor :minHeight="300" v-model="form.content"></Editor>
        </el-form-item>
        <el-form-item label="设为置顶" prop="pinStatus">
          <el-switch
            active-value="0"
            inactive-value="1"
            v-model="form.pinStatus"
            active-color="#13ce66"
            inactive-color="#f5f5f5"
          ></el-switch>
        </el-form-item>
        <!-- remark -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
    
    <script>
import {
  ageBulletinList,
  delBulletin,
  addBulletin,
  updateBulletin,
  bulletinTypeList,
  addBulletinType,
  updateBulletinType,
  delBulletinType
} from "@/api/notice/index";
import { deptUserTree } from "@/api/reportTm";
import Editor from "@/components/Editor";
export default {
  name: "Notice",
  components: {
    Editor
  },
  dicts: [],
  data() {
    return {
      supplierSubjectsIdProps: {
        multiple: true,
        label: "label",
        value: "id",
        children: "children"
      },
      subjectList: [],
      dialogFormVisible: false,
      typeForm: {
        bulletinTypeName: ""
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        docCode: undefined,
        scheduleStatus: undefined
      },
      // 表单参数
      form: {
        visibilityType: "0"
      },
      bulletinId: "",
      // 表单校验
      rules: {
        title: [{ required: true, message: "不能为空", trigger: "blur" }],
        category: [{ required: true, message: "不能为空", trigger: "blur" }],
        visibilityList: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        content: [{ required: true, message: "不能为空", trigger: "blur" }]
      },
      bulletinTypeData: [],
      bulletinTypeId: ""
    };
  },
  created() {
    this.bulletinTypeListFn();
    this.getTreeList();
    this.getList();
  },
  methods: {
    getTreeList() {
      deptUserTree().then(response => {
        this.subjectList = response.data;
      });
    },
    openType(item) {
      this.dialogFormVisible = true;
      if (item) {
        this.bulletinTypeId = item.bulletinTypeId;
        this.typeForm.bulletinTypeName = item.bulletinTypeName;
      }
    },
    hendType(item) {
      this.typeIndex = item.bulletinTypeId;
      this.handleQuery();
    },
    typeConfig() {
      if (!this.typeForm.bulletinTypeName)
        return this.$message.error("类型名称不能为空");
      if (this.bulletinTypeId) {
        updateBulletinType({
          ...this.typeForm,
          bulletinTypeId: this.bulletinTypeId
        }).then(res => {
          if (res.code == 200) {
            this.typeForm = {
              bulletinTypeName: ""
            };
            this.bulletinTypeId = "";
            this.dialogFormVisible = false;
            this.bulletinTypeListFn();
            this.handleQuery();
          }
        });
      } else {
        addBulletinType(this.typeForm).then(res => {
          if (res.code == 200) {
            this.typeForm = {
              bulletinTypeName: ""
            };
            this.dialogFormVisible = false;
            this.bulletinTypeListFn();
            this.bulletinTypeId = "";
            this.handleQuery();
          }
        });
      }
    },
    /** 删除按钮操作 */
    handleTypeDelete(row) {
      const bulletinTypeIds = row.bulletinTypeId;
      this.$modal
        .confirm("是否删除该条数据？")
        .then(function() {
          return delBulletinType(bulletinTypeIds);
        })
        .then(() => {
          this.bulletinTypeListFn();
          this.handleQuery();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    bulletinTypeListFn() {
      bulletinTypeList().then(res => {
        if (res.code == 200) {
          this.bulletinTypeData = res.rows;
        }
      });
    },
    /** 查询字典类型列表 */
    getList() {
      this.loading = true;
      ageBulletinList(this.addDateRange(this.queryParams, this.dateRange)).then(
        response => {
          this.typeList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        content: undefined,
        title: undefined,
        visibility: undefined,
        visibilityType: "0",
        visibilityList: [],
        ossfile: [],
        file: "",
        pinStatus: "1"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.bulletinId);
      this.single = selection.length != 1;
      this.multiple = !selection.length;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.bulletinId = row.bulletinId;
      this.form = {
        ...row,
        visibilityType: row.visibility == "0" ? "0" : "1"
      };
      this.open = true;
      this.title = "修改";
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let params = JSON.parse(JSON.stringify(this.form));
          if (params.visibilityType == 0) {
            params.visibility = 0;
          } else {
            let visibility = [];
            params.visibilityList.forEach(v => {
              visibility.push(v[v.length - 1]);
            });
            params.visibility = visibility.join(",");
          }
          if (this.bulletinId) {
            updateBulletin(params).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBulletin(params).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bulletinIds = row.bulletinId || this.ids;
      this.$modal
        .confirm("是否删除该条数据？")
        .then(function() {
          return delBulletin(bulletinIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/dict/type/export",
        {
          ...this.queryParams
        },
        `type_${new Date().getTime()}.xlsx`
      );
    }
  }
};
</script>

<style scoped>
.typeActive {
  background: #f6f9fe;
}
</style>
    